package com.breaktian.hybrid.modules.ui;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.breaktian.hybrid.base.BaseCordovaAction;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.logging.Handler;

/**
 * Created by keke.tian on 2018/2/5.
 */

public class ToastAction implements BaseCordovaAction {


    @Override
    public boolean execAction(CordovaPlugin cordovaPlugin, Activity activity, JSONArray args, final CallbackContext callbackContext) {
        Log.i("cordova args: ",args.toString());
        try {
            JSONObject json = args.getJSONObject(0);
            String message = json.getString("message");

            toast(message,activity);

//            callbackContext.success("{\"success\":\"true\"}");
//            new android.os.Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    callbackContext.error("{\"status\":\"error\"}");
//                }
//            }, 3000);

            callbackContext.success("{\"status\":\"success\"}");
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return true;
    }

    private void toast(final String msg,final Context context){
        new android.os.Handler().post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

}
